package javachess;

public class King extends Piece
{
	public King(int a_owner, int a_x, int a_y)
	{
		super ('K', a_owner);
		m_owner = a_owner;
		m_x = a_x;
		m_y = a_y;
	}
	/**
	 * -Takes in the x and y destination to determine if the move is acceptable.
	 * -Returns a boolean to indicate 
	 * @param a_x
	 * @param a_y
	 * @return
	 */
	public boolean validateMove(Player player, Player otherPlayer, int a_x, int a_y)
	{
		//this is checking to see if you are trying to move on top of your own piece
		for(int i = 0; i < player.m_pieces.size(); ++i)
		{
			if(player.m_pieces.get(i).m_x == a_x 
			&& player.m_pieces.get(i).m_y == a_y)
			{
				return false;
			}
		}
		//just adding comments to help myself understand how this logic works
		// a_x = the x location of where you want to move
		// a_y = the y location of where you want to move
		// a_x + 1 and a_y + 1 means that you clicked the upper left spot 0, 0 for example
		//  adding 1 to each would mean that the spot the king needed to be at was 1, 1
		// if the king was at 1, 1 then 0,0 is a valid spot to move to
		//actually moving the piece is separated from validateMove
		if(a_x + 1 == m_x && a_y + 1 == m_y
		|| a_x + 0 == m_x && a_y + 1 == m_y
		|| a_x - 1 == m_x && a_y + 1 == m_y
		|| a_x - 1 == m_x && a_y + 0 == m_y
		|| a_x - 1 == m_x && a_y - 1 == m_y
		|| a_x + 0 == m_x && a_y - 1 == m_y
		|| a_x + 1 == m_x && a_y - 1 == m_y
		|| a_x + 1 == m_x && a_y + 0 == m_y)
			return true;
		return false;
		/*
		//these must remain in this scope in order to be reset if a swap occurred
		int temp_x = m_x, temp_y = m_y;
		//if the destination x is less than the object x, swap them
		if(a_x < m_x)
		{
			m_x = a_x;
			a_x = temp_x;
		}
		//if the destination y is less than the object y, swap them
		if(a_y < m_y)
		{
			m_y = a_y;
			a_y = temp_y;
		}
		//if the king has moved diagonally square
		if((a_x - m_x) == 1 && (a_y - m_y) == 1)
		{
			//in case of swap
			m_x = temp_x;
			m_y = temp_y;
			for(int i = 0; i < player.m_pieces.size(); ++i)
			{
				//if the location is equal to one of the player's own pieces
				if(player.m_pieces.get(i).m_x == a_x && player.m_pieces.get(i).m_y == a_y )
					return false;
				//if the location is equal to another player's piece
				if(otherPlayer.m_pieces.get(i).m_x == a_x && otherPlayer.m_pieces.get(i).m_y == a_y )
					return true;
			}
			return true;
		}
		//if the king moved horizontally one square
		else if((a_x - m_x) == 1 && (a_y - m_y) == 0)
		{
			//in case of swap
			m_x = temp_x;
			m_y = temp_y;
			for(int i = 0; i < player.m_pieces.size(); ++i)
			{
				//if the location is equal to one of the player's own pieces
				if(player.m_pieces.get(i).m_x == a_x && player.m_pieces.get(i).m_y == a_y )
					return false;
			}
			for(int i = 0; i < otherPlayer.m_pieces.size(); ++i)
			{
				//if the location is equal to another player's piece
				if(otherPlayer.m_pieces.get(i).m_x == a_x && otherPlayer.m_pieces.get(i).m_y == a_y )
					return true;
			}
			return true;
		}
		//if the king moved vertically one square
		else if((a_x - m_x) == 0 && (a_y - m_y) == 1)
		{
			//in case of swap
			m_x = temp_x;
			m_y = temp_y;
			for(int i = 0; i < player.m_pieces.size(); ++i)
			{
				//if the location is equal to one of the player's own pieces
				if(player.m_pieces.get(i).m_x == a_x && player.m_pieces.get(i).m_y == a_y )
					return false;
			}
			for(int i = 0; i < otherPlayer.m_pieces.size(); ++i)
			{
				//if the location is equal to another player's piece
				if(otherPlayer.m_pieces.get(i).m_x == a_x && otherPlayer.m_pieces.get(i).m_y == a_y )
					return true;
			}
		}
		//in case of swap
		m_x = temp_x;
		m_y = temp_y;
		return false;
		*/
	}
	
}
